<?php
include $_SERVER['DOCUMENT_ROOT'].'/inc/env.inc.php';

$siteComponent = new Site();

$label = str_replace(" ", "-", $_POST["name"]);
$label = str_replace("à", "a", $label);
$label = str_replace("è", "e", $label);
$label = str_replace("ì", "i", $label);
$label = str_replace("ò", "o", $label);
$label = str_replace("ù", "u", $label);

$i = 2;
$newLabel = $label;
while (!$siteComponent->searchLabelPage($newLabel)) {
	$newLabel = $label.'-'.$i;
	$i++;
}
$sql = "INSERT INTO ".$db->getPrefix()."pages (parent, label, name, title, date_in) 
		VALUES ('".$_POST["idParent"]."', '".$newLabel."', '".$_POST["name"]."', '".$_POST["name"]."', now())";

$db->query($sql);

$page = new Page($newLabel);
echo '$(location).attr("href", "'.$page->get('path').$newLabel.'.html");';
?>